//$URL: http://dasding.googlecode.com/svn/branches/spring-security/data/data/src/main/java/de/piratech/dasding/data/Event.java $
//$Id: Event.java 68 2012-10-19 21:43:35Z amuthmann@gmail.com $
/*
 * Copyright 2012 Piratech.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.piratech.dasding.data;

import de.micromata.opengis.kml.v_2_2_0.Document;
import de.micromata.opengis.kml.v_2_2_0.Folder;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlIDREF;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * An event
 *
 * @author deveth0
 */
@XmlRootElement
public class Event extends AbstractAction {

  

  public enum EVENTSTATUS {

    ACTIVE,
    CANCELED,
    FINISHED
  }
  private List<Action> actions = new ArrayList<Action>();
  private List<User> prospects = new ArrayList<User>();
  private EVENTSTATUS status = EVENTSTATUS.ACTIVE;
  private Event predecessor;
  private EventCategory category;

  @XmlElementWrapper
  @XmlElement(name = "action")
  public List<Action> getActions() {
    return actions;
  }

  public void addAction(Action action) {
    actions.add(action);
  }

  public void setActions(List<Action> actions) {
    this.actions = actions;
  }

  @XmlElementWrapper
  @XmlElement(name = "user")
  @XmlIDREF
  public List<User> getProspects() {
    return prospects;
  }

  public void setProspects(List<User> prospects) {
    this.prospects = prospects;
  }

  public EVENTSTATUS getStatus() {
    return status;
  }

  public void setStatus(EVENTSTATUS status) {
    this.status = status;
  }

  @XmlIDREF
  public Event getPredecessor() {
    return predecessor;
  }

  public void setPredecessor(Event predecessor) {
    this.predecessor = predecessor;
  }

  public EventCategory getCategory() {
    return category;
  }

  public void setCategory(EventCategory category) {
    this.category = category;
  }

  @Override
  public boolean isValid() {
    return super.isValid() && category != null;
  }
  
  public Document getKML() {
    final Document document = new Document();
    document.createAndAddPlacemark().withName(getName())
            .withDescription(getDescription())
            .createAndSetPoint().addToCoordinates(getLocation().getLongitude(), getLocation().getLatitude());

    if (!getActions().isEmpty()) {
      Folder acts = document.createAndAddFolder().withName("actions");
      for (Action action : getActions()) {
        acts.createAndAddPlacemark().withName(action.getName())
                .withDescription(action.getDescription())
                .createAndSetPoint().addToCoordinates(action.getLocation().getLongitude(), action.getLocation().getLatitude());
      }
    }
    return document;
  }
}
